package com.zhuanzhuan.hero.user.util;

import com.zhuanzhuan.hero.user.consts.NumConsts;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName StringUtil
 * @Description
 * @Author hanjunjun
 * @Date 2024/6/21 14:25
 */
public class StringUtil {

    private static final String SPLIT_CHAR = ",";

    private static final int MAX_DISPLAY_NUM = 10000;

    /**
     * 逗号分隔的字符串转为list
     *
     * @param content
     * @return java.util.List<java.lang.String>
     * @author  hanjunjun
     * @date 2024/6/21
     **/
    public static List<String> str2List(String content) {
        return str2List(content, SPLIT_CHAR);
    }

    public static String list2Str(List<String> list) {
        return list2Str(list, SPLIT_CHAR);
    }

    /**
     * 11000处理成1.1W
     *  
     * @param num
     * @return java.lang.String
     * @author  hanjunjun  
     * @date 2024/7/12       
     **/
    public static String convertNum(Long num) {
        if (Objects.isNull(num)) {
            return "";
        }
        if (num < MAX_DISPLAY_NUM) {
            return String.valueOf(num);
        }

        return BigDecimalUtil.divide(String.valueOf(num), String.valueOf(MAX_DISPLAY_NUM), NumConsts.ONE) + "W";
    }


    private static List<String> str2List(String content, String splitChar) {
        if (StringUtils.isBlank(content)) {
            return new ArrayList<>();
        }

        return Arrays.asList(content.split(splitChar));
    }

    private static String list2Str(List<String> list, String splitChar) {
        if (CollectionUtils.isEmpty(list)) {
            return "";
        }

        return String.join(splitChar, list);
    }

    /**
     * 如果为null则返回""
     *
     * @param source
     * @return java.lang.String 不会返回null
     * @author  hanjunjun
     * @date 2024/8/5
     **/
    public static String getOrEmpty(String source) {
        return Objects.isNull(source) ? StringUtils.EMPTY : source;
    }
}
